define(['../../../../js/lib/other/sweet-alert','../../carrys/md5'],function() {
    return ['$scope','$location', '$interval','$timeout','bizProxy', function($scope,$location,$interval,$timeout,bizProxy) {
		$scope.$emit("top-show",'true');//顶部显示
    	$scope.$emit('menuTypeLight','mine'); 	//控制底部menu高亮
    	$scope.$emit('bub-menu','true'); 	//隐藏底部menu
    	$scope.$emit('flag-tag','10');//显示底部文字

		//默认值
        $scope.mobileNo = transData.mobileNo;
        $scope.password = "";
        $scope.isactive = false;

        $scope.$watch('password', function() {
            if(($scope.password).length > 0){
                changeStyle();
            }
        });

        $scope.$watch('mobileNo', function() {
            if(($scope.mobileNo).length > 0){
                changeStyle();
            }
        });

        $scope.goRegister=function(){
            $scope.$emit('goNext','/register');
        }
        //点击登录
        $scope.goLogin = function(){
            if(! _SYT_util.isValidNo($scope.mobileNo)){
                $scope.$emit("promptAlertMsg","手机号无效");
                $scope.$emit("showCollect",true);
                $timeout(function(){
                    $scope.$emit("showCollect",false);
                },transData.promptTime);
                return;
            }
            if(!($scope.password+"")){
                $scope.$emit("promptAlertMsg","请输入密码");
                $scope.$emit("showCollect",true);
                $timeout(function(){
                    $scope.$emit("showCollect",false);
                },transData.promptTime);
                return;
            }
            startLogin();
        }

        //切换样式
        function changeStyle(){
            if(($scope.mobileNo).length == 11 && ($scope.password).length > 0){
                $scope.isactive = true;
            }else{
                $scope.isactive = false;
            }
        }

        function startLogin(){
            var headerInfo = {},bizInfo = {};

            //用户登录
            headerInfo.pageNum = '';
            headerInfo.pageSize = '';
            headerInfo.sessionId = '';

            if(_YHC_isWEIXIN.isWeiXin()){
                console.log("isWeiXin"+localStorage.getItem("sessionId"));
                console.log("开始微信登陆绑定");
                bizInfo = transData.authInfo;
            }else{
                console.log("不是微信环境");
                bizInfo.loginType = "password";
                bizInfo.thirdLoginId = "";
            }
            bizInfo.mobileNo = $scope.mobileNo;
            bizInfo.password = $scope.password;
            /*if(locationCityInfo.detCityName != "" && locationCityInfo.district != ""){
                bizInfo.lbs = locationCityInfo.detCityName + "_" + locationCityInfo.district;
            }else{
                bizInfo.lbs = "";
            }*/
            $scope.$emit("loadingChangeTpr",true);
            bizProxy.queryProxyBizData("userLogin", headerInfo,bizInfo,
                function(tempData){
                    console.log(tempData);
                    $scope.$emit("loadingChangeTpr",false);
                    if(tempData.sessionId != "" && tempData.sessionId != null){
                        $timeout(function(){
                            localStorage.setItem("fireFlySessionId",tempData.sessionId+"");
                            $scope.$emit("loadingChangeTpr",false);
                            var body = JSON.parse(tempData.body);
                            transData.mobileNo = body.mobileNo;
                            transData.sessionId = tempData.sessionId;
                            transData.userId = body.userId;
                            resultData.loginResultData = body;
                            queryUserExt();
                        },0);
                    }
                },function(data){
                    $scope.$emit("loadingChangeTpr",false);
                    if(data != ""){
                        if(data.errorCode == ERROR_CODE.notExist){
                            $scope.$emit("promptAlertMsg",data.errorMsg);
                            $scope.$emit("showCollect",true);
                            $timeout(function(){
                                $scope.$emit("showCollect",false);
                                transData.password = "",transData.mobileNo = "";
                            },transData.promptTime);
                        }else if(data.errorCode == ERROR_CODE.pwdNotMatch){
                            $scope.$emit("promptAlertMsg",data.errorMsg);
                            $scope.$emit("showCollect",true);
                            $timeout(function(){
                                $scope.$emit("showCollect",false);
                            },transData.promptTime);
                        }else{
                            $scope.$emit("promptAlertMsg",data.errorMsg);
                            $scope.$emit("showCollect",true);
                            $timeout(function(){
                                $scope.$emit("showCollect",false);
                                transData.password = "",transData.mobileNo = "";
                            },transData.promptTime);
                        }
                    }else{
                        $scope.$emit("loadingChangeTpr",false);
                        $scope.$emit("promptAlertMsg","连接超时");
                        $scope.$emit("showCollect",true);
                        $timeout(function(){
                            $scope.$emit("showCollect",false);
                            transData.password = "",transData.mobileNo = "";
                            $scope.$emit("goNext","/mine");
                        },transData.promptTime);
                    }
                }
            );

        }
        //=====================================
        //默认值
        $scope.mobileNo2 = transData.mobileNo;
        $scope.authCode = "";
        $scope.yzmLight = false;

        $scope.showSend = true;
        $scope.showTime = false;
        $scope.isactive = false;
        $scope.countdown = 60;
        var tempCheckNo = "";var timeOutId;

        //监听手机号是否要高亮
        $scope.$watch('mobileNo2', function() {
            if(($scope.mobileNo2).length > 0){
                if(($scope.mobileNo2).length == 11){
                    $scope.yzmLight = true;
                }else{
                    $scope.yzmLight = false;
                }
                changeStyle2();
            }
        });

        $scope.$watch('authCode', function() {
            if(($scope.authCode + '').length > 0){
                changeStyle2();
            }
        });

        //点击获取验证码
        $scope.getAuthCode = function(){
            if(! _SYT_util.isValidNo($scope.mobileNo2)){
                $scope.$emit("promptAlertMsg","手机号无效");
                $scope.$emit("showCollect",true);
                $timeout(function(){
                    $scope.$emit("showCollect",false);
                },transData.promptTime);
                return;
            }
            $scope.showSend = false;
            $scope.showTime = true;
            getAuthCode();
        }

        //去下一步
        $scope.goNext = function(){
            if(!($scope.authCode+"")){
                $scope.$emit("promptAlertMsg","请输入验证码");
                $scope.$emit("showCollect",true);
                $timeout(function(){
                    $scope.$emit("showCollect",false);
                },transData.promptTime);
                return;
            }
            //验证验证码
            if(tempCheckNo == hex_md5($scope.authCode+"")){
                transData.mobileNo = $scope.mobileNo2;
                transData.mobileCheckNo = $scope.authCode+"";
                startLogin2();
            }else{
                $scope.$emit("promptAlertMsg","验证码错误");
                $scope.$emit("showCollect",true);
                $timeout(function(){
                    $scope.$emit("showCollect",false);
                },transData.promptTime);
                transData.mobileCheckNo = "";
                $scope.authCode = "";
            }
        }


        function startLogin2(){
            var headerInfo = {},bizInfo = {};
            //用户登录
            headerInfo.pageNum = '';
            headerInfo.pageSize = '';
            headerInfo.sessionId = '';


            if(_YHC_isWEIXIN.isWeiXin()){
                console.log("isWeiXin"+localStorage.getItem("sessionId"));
                console.log("开始微信登陆绑定");
                bizInfo = transData.authInfo;
            }else{
                console.log("不是微信环境");
                bizInfo.loginType = "checkNo";
                bizInfo.thirdLoginId = "";
            }

            bizInfo.mobileNo = $scope.mobileNo2;
            bizInfo.mobileCheckNo = $scope.authCode+"";
            /*if(locationCityInfo.detCityName != "" && locationCityInfo.district != ""){
                bizInfo.lbs = locationCityInfo.detCityName + "_" + locationCityInfo.district;
            }else{
                bizInfo.lbs = "";
            }*/
            $scope.$emit("loadingChangeTpr",true);
            bizProxy.queryProxyBizData("userLogin", headerInfo,bizInfo,
                function(tempData){
                    $scope.$emit("loadingChangeTpr",false);
                    if(tempData.sessionId != "" && tempData.sessionId != null){
                        $timeout(function(){
                            console.log(tempData);
                            localStorage.setItem("fireFlySessionId",tempData.sessionId+"");
                            $scope.$emit("loadingChangeTpr",false);
                                var body = JSON.parse(tempData.body);
                                transData.mobileNo = body.mobileNo;
                                transData.sessionId = tempData.sessionId;
                                transData.userId = body.userId;
                                resultData.loginResultData = body;
	                            queryUserExt();
                        },0);
                    }

                },function(data){
                    $scope.$emit("loadingChangeTpr",false);
                    if(data != ""){
                        if(data.errorCode == ERROR_CODE.notExist){
                            $scope.$emit("promptAlertMsg",data.errorMsg);
                            $scope.$emit("showCollect",true);
                            $timeout(function(){
                                $scope.$emit("showCollect",false);
                                transData.password = "",transData.mobileNo = "";
                            },transData.promptTime);
                        }else if(data.errorCode == ERROR_CODE.pwdNotMatch){
                            $scope.$emit("promptAlertMsg",data.errorMsg);
                            $scope.$emit("showCollect",true);
                            $timeout(function(){
                                $scope.$emit("showCollect",false);
                            },transData.promptTime);
                        }else{
                            $scope.$emit("promptAlertMsg",data.errorMsg);
                            $scope.$emit("showCollect",true);
                            $timeout(function(){
                                $scope.$emit("showCollect",false);
                                transData.password = "",transData.mobileNo = "";
                            },transData.promptTime);
                        }
                    }else{
                        $scope.$emit("loadingChangeTpr",false);
                        $scope.$emit("promptAlertMsg","连接超时");
                        $scope.$emit("showCollect",true);
                        $timeout(function(){
                            $scope.$emit("showCollect",false);
                            transData.password = "",transData.mobileNo = "";
                            $scope.$emit("goNext","/mine");
                        },transData.promptTime);
                    }
                }
            );

        }
        
        //查询用户扩展信息
        function queryUserExt(){
        	var headerInfo = {},bizInfo = {};
        	headerInfo.pageNum = '';
        	headerInfo.pageSize = '';
        	headerInfo.sessionId = transData.sessionId;
    		$scope.$emit("loadingChange",true);
    		bizProxy.queryProxyBizData("queryUserExt",headerInfo,bizInfo,
    				function(tempData){//SUCCESS
                        console.log("查询用户信息=",tempData);
    					$scope.$emit("loadingChange",false);
						$timeout(function(){
                            var body = JSON.parse(tempData.body);
                            transData.userType=body.userType;
                            resultData.loginUserData=body;
                            if(transData.userType === '1'){
                                $scope.$emit("goNext","/mineLs");
                            }else{
                                $scope.$emit("goNext","/minePt");
                            }
						},100);
		    		},function(data){
    				    console.log(data)
		    			if(data != ""){//FAIL
		    				$scope.$emit("loadingChange",false);
							if(data.errorCode == ERROR_CODE.noLogin){
								FIREFLY("", data.errorMsg,"",function(){
										//调用退出登录
										transData.mobileNo = "";
		    							transData.password = "";
		    							transData.sessionId = "";
		    							transData.userType = "";
		    							resultData.loginUserData = "";
		    							$timeout(function(){
		    								$scope.$emit("goNext","/mine");
		    							},0);
								});
							}else{
								$scope.$emit("promptAlertMsg",data.errorMsg);
								$scope.$emit("showCollect",true);
								$timeout(function(){
									$scope.$emit("showCollect",false);
								},transData.promptTime);
							}
		    			}else{
		    				$scope.$emit("loadingChange",false);
							$scope.$emit("promptAlertMsg","连接超时");
							$scope.$emit("showCollect",true);
							$timeout(function(){
								$scope.$emit("showCollect",false);
							},transData.promptTime);
		    			}
		    		});
        }
        
        

        //请求代理 获取验证码
        function getAuthCode(){
            startCountDown();
            var headerInfo = {},bizInfo = {};
            headerInfo.pageNum = '';
            headerInfo.pageSize = '';
            headerInfo.sessionId =transData.sessionId;

            bizInfo.mobileNo = $scope.mobileNo2;
            bizInfo.smsKind = "checkNo";
            bizProxy.queryProxyBizData("sendCheckNo", headerInfo,bizInfo,
                function(tempData){
                    tempCheckNo = tempData.body;
                },
                function(data){
                    if(data != ""){//FAIL
                        $scope.$emit("promptAlertMsg",data.errorMsg);
                        $scope.$emit("showCollect",true);
                        $timeout(function(){
                            $scope.$emit("showCollect",false);
                        },transData.promptTime);
                    }else{
                        $scope.$emit("promptAlertMsg","连接超时");
                        $scope.$emit("showCollect",true);
                        $timeout(function(){
                            $scope.$emit("showCollect",false);
                        },transData.promptTime);
                    }
                }
            );
        }

        //切换样式
        function changeStyle2(){
            if(($scope.mobileNo2).length == 11 && ($scope.authCode + '').length == 6){
                $scope.isactive = true;
            }else{
                $scope.isactive = false;
            }
        }

        //开始倒计时
        function startCountDown(){
            timeOutId = $interval(function(){
                if (--$scope.countdown == 0){
                    stopCountDown();
                    $scope.countdown = 60;
                    $scope.showSend = true;
                    $scope.showTime = false;
                }
            }, 1000);
        }

        //停止倒计时
        function stopCountDown() {
            $interval.cancel(timeOutId);
        }







        $scope.loginTab=true;

        $scope.pwdLogin=function(){
            $scope.loginTab=true;
        }
        $scope.codeLogin=function(){
            $scope.loginTab=false;
        }

		//=============================================================================================================
/*
        //默认值
        $scope.mobileNo = transData.mobileNo,$scope.isactive = false;
        $scope.password = _YHC_util.strToArr("");
        var tempRandom = 2.1,tempPassword = "";
        $scope.loginTab=true;
        $scope.pwdLogin=function () {
            $scope.loginTab=true;
        }
        $scope.codeLogin=function () {
            $scope.loginTab=false;
        }

        document.querySelector("#log_in .top_area .ipt_area input").addEventListener('click',function(){
            $timeout(function(){
                $scope.$emit("pwdSlidUp",'1');
            },0);
        });

        $scope.wxLogin = function(){
            share.thirdLogin("WEIXIN",function(ret){console.log(ret)},function(){});
        }

        //监听子集显示顶部显示
        $scope.$on('pwdChange', function(event, data) {
            $timeout(function(){
                if(data.length == 6){
                    tempPassword = data;
                }
                $scope.password = _YHC_util.strToArr(data);
            },0);
        });

        //点击获取密码数据
        $scope.getPassword = function(){
            var tempTime = 500;
            if(_YTG_IOSorANDROID.systemJudge() == 'IOS'){
                tempTime = 1000;
            }

            $timeout(function(){
                $scope.$emit("pwdSlidUp",tempRandom++);
            },tempTime);
        }

        //找回密码
        $scope.findPwd = function(){
            $scope.$emit("goNext","/findPwd");
        }

        //用户注册
        $scope.registerUser = function(){
            $scope.$emit("goNext","/register");
        }

        //直接登录
        $scope.logining = function(){
            if(! _SYT_util.isValidNo($scope.mobileNo)){
                $scope.$emit("promptAlertMsg","手机号无效");
                $scope.$emit("showCollect",true);
                $timeout(function(){
                    $scope.$emit("showCollect",false);
                },transData.promptTime);
                return;
            }
            goLogin();
        }

        //监听是否需要高亮下一步
        $scope.$watch('mobileNo', function() {
            if(($scope.mobileNo).length > 0){
                changeStyle();
            }
        });
        $scope.$watch('password', function() {
            if(($scope.password).length > 0){
                changeStyle();
            }
            if(($scope.password)[5] != ""){
                $scope.$emit("pwdSlidUp",'0');
            }
        });

        //切换样式
        function changeStyle(){
            if(($scope.mobileNo).length == 11 && ($scope.password)[5] != ""){
                $scope.isactive = true;
            }else{
                $scope.isactive = false;
            }
        }

        //去登陆
        function goLogin(){
            transData.mobileNo = $scope.mobileNo;
            transData.password = tempPassword;

            var headerInfo = {},sendData = {};
            headerInfo.pageNum = '';
            headerInfo.pageSize = '';
            headerInfo.sessionId = transData.sessionId;

            sendData.loginType = "password";
            sendData.mobileNo = transData.mobileNo;
            sendData.password = transData.password;
            $scope.$emit("loadingChangeTpr",true);
            bizProxy.queryProxyBizData("userLogin",headerInfo,sendData,
                    function(tempData){//SUCCESS
                        localStorage.setItem("fireFlySessionId",tempData.sessionId+"");
                        $scope.$emit("loadingChangeTpr",false);
                        console.log(tempData.body)
                        var body = JSON.parse(tempData.body);
                        transData.mobileNo = body.mobileNo;
                        transData.sessionId = tempData.sessionId;
                        transData.userId = body.userId;
                        transData.userType = body.userType;
                        resultData.loginUserData = body;
                        $timeout(function(){
                            if(transData.gotoUrl != ""){
                                $location.path("/login").replace();//想改变当前 URL 而不创建新的历史记录的话
                                if(transData.gotoUrl == "/fireLoanForm"){
                                    transData.gotoUrl = "";
                                    $scope.$emit('goNext',"/fireLoanForm");
                                }else if(transData.gotoUrl == "/caseSrcLib"){
                                    transData.gotoUrl = "";
                                    $scope.$emit('goNext',"/caseSrcLib/2");
                                }else if(transData.gotoUrl == "/caseProfile"){
                                    transData.gotoUrl = "";
                                    $scope.$emit('goNext',"/caseProfile");
                                }
                            }else{
                                if(transData.userType === '1'){
                                    $scope.$emit('goNext',"/mineLs");
                                }else if(transData.userType === '0'){
                                    $scope.$emit('goNext',"/minePt");
                                }
                            }

                        },0);
                    },function(data){//FAIL
                        $scope.$emit("loadingChangeTpr",false);
                        if(data != ""){
                            if(data.errorCode == ERROR_CODE.notExist){
                                $scope.$emit("promptAlertMsg",data.errorMsg);
                                $scope.$emit("showCollect",true);
                                $timeout(function(){
                                    $scope.$emit("showCollect",false);
                                    transData.password = "",transData.mobileNo = "";
                                },transData.promptTime);
                            }else if(data.errorCode == ERROR_CODE.pwdNotMatch){
                                $scope.$emit("promptAlertMsg",data.errorMsg);
                                $scope.$emit("showCollect",true);
                                $timeout(function(){
                                    $scope.$emit("showCollect",false);
                                },transData.promptTime);
                            }else{
                                $scope.$emit("promptAlertMsg","连接超时");
                                $scope.$emit("showCollect",true);
                                $timeout(function(){
                                    $scope.$emit("showCollect",false);
                                    transData.password = "",transData.mobileNo = "";
                                },transData.promptTime);
                            }
                        }else{
                            $scope.$emit("loadingChangeTpr",false);
                            $scope.$emit("promptAlertMsg","连接超时");
                            $scope.$emit("showCollect",true);
                            $timeout(function(){
                                $scope.$emit("showCollect",false);
                                transData.password = "",transData.mobileNo = "";
                                $scope.$emit("goNext","/mine");
                            },transData.promptTime);
                        }

                    });
        }*/

    	//当键盘被销毁的时候
        $scope.$on("$destroy", function() {
        	transData.gotoUrl = "";
        	$scope.$emit("pwdSlidUp","");
	    });
    	
        $scope.$apply();
    }];
});